<template>
  <div>
    <div id="search">
      <el-form :inline="true" :model="ClassSearch" class="demo-form-inline">
        <el-form-item label="班主任">
          <el-input v-model="ClassSearch.principal" placeholder="请输入班主任姓名"></el-input>
        </el-form-item>
        <el-form-item label="授课老师">
          <el-input v-model="ClassSearch.lecturer" placeholder="请输入授课老师姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="medium" icon="el-icon-search" @click="onSearchSubmit">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div id="header-bth">
      <el-button type="primary" size="small" icon=el-icon-circle-plus-outline @click="showAddClass">添加</el-button>
    </div>
    <div id="role-table">
      <el-table
          :header-cell-style="{background:'rgb(242, 242, 242)'}"
          style="width: 100%;text-align: center"
          :data="tableData"
          :row-style="{height: '50px'}"
          :cell-style="{padding: '0'}"
      >
        <el-table-column label="ID">
          <template slot-scope="scope">
            <span>{{ scope.row.id}}</span>
          </template>
        </el-table-column>
        <el-table-column label="班主任" >
          <template slot-scope="scope">
            <span >{{ scope.row.principal}}</span>
          </template>
        </el-table-column>
        <el-table-column label="授课老师">
          <template slot-scope="scope">
            <span>{{ scope.row.lecturer}}</span>
          </template>
        </el-table-column>
        <el-table-column label="开班时间" >
          <template slot-scope="scope">
            <span>{{ scope.row.classTime}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                icon="el-icon-edit"
                size="mini"
                @click="handleEdit(scope.$index, scope.row)">
            </el-button>
            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div id="nav-page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="pageSizes"
          :current-page="ClassSearch.page"
          :page-size="ClassSearch.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!--添加 编辑班级信息弹窗-->
    <el-dialog :title="dialogTitle" :visible.sync="addScoreFormVisible" width="40%" @close="clearInfo">
      <el-form :model="ScoreForm" ref="courseInfo" :rules="CourseRules" label-width="100px" size="medium">
        <el-form-item label="班主任姓名" prop="principal">
          <el-input v-model="ScoreForm.principal" autocomplete="off"  placeholder="请输入班主任姓名"></el-input>
        </el-form-item>
        <el-form-item label="授课老师" prop="lecturer">
          <el-input v-model="ScoreForm.lecturer" autocomplete="off"  placeholder="请输入授课老师姓名"></el-input>
        </el-form-item>
        <el-form-item label="入学日期" prop="classTime">
          <el-date-picker
              v-model="ScoreForm.classTime"
              type="datetime"
              placeholder="选择开班时间"
              value-format="yyyy-MM-dd HH:mm:ss">
          </el-date-picker>

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit('courseInfo')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import instance from "@/request/request";

export default {
  data() {
    return {
      tableData: [],
      pageSizes: [3, 5, 10, 15],
      total: 0,
      addScoreFormVisible: false,
      dialogTitle: '',
      ScoreForm: {},
      ClassSearch: {  // 搜索数据
        page: 1,
        limit: 5
      },
      CourseRules: {
        principal: [{required: true, message: '请输入班主任', trigger: 'blur'}],
        lecturer: [{required: true, message: '请输入授课老师', trigger: 'blur'}],
        classTime:[{ required: true, message: '请选择开班日期', trigger: 'change' }],
      },

    }
  },
  created() {
    this.getscoreList()
  },
  methods: {
    //获取开班计划信息
    getscoreList() {
      instance.get("/schedule/getlist",{params:this.ClassSearch}).then(res=>{
        if(res.code==200){
          this.tableData=res.data.schedule
          this.total=res.data.total
          return
        }
        this.$message.error(res.msg)
      }).catch(erro=>{
        this.$message.error("请求失败！")
      })
    },
    // 编辑角色
    handleEdit(index, row) {
      this.dialogTitle = "编辑"
      this.addScoreFormVisible= true
      var course=Object.assign({},row)
      this.ScoreForm=course
    },
    //更新和添加用户信息
    submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          instance.post("/schedule/addorupdate",this.ScoreForm).then(res=>{
            if (res.code==200){
              this.$message.success(res.msg)
              this.addScoreFormVisible=false
              this.getscoreList()
              return
            }
            this.$message.warning(res.msg)
          }).catch(erro=>{
            this.$message.error("请求失败")
          })

        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    // 删除角色
    handleDelete(id) {
      this.$confirm('确认要删除？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const ids=[id]
        instance.delete("/schedule/delete/",{data:ids}).then(res=>{
          if(res.code==200){
            this.$message.success(res.msg)
            this.getscoreList()
            return
          }
          this.$message.warning(res.msg)
        }).catch(erro=>{
          this.$message.error("请求失败！")
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消！'
        })
      })
    },
    // 搜索角色
    onSearchSubmit() {
      this.ClassSearch.page = 1
      this.ClassSearch.limit = 5
      this.getscoreList()
    },
    // 改变每页显示的条数触发
    handleSizeChange(val) {
      this.ClassSearch.limit = val;
      this.getscoreList()
    },
    // 改变页码触发
    handleCurrentChange(val) {
      this.ClassSearch.page = val;
      this.getscoreList()
    },
    // 显示弹出等
    showAddClass() {
      this.dialogTitle = "添加班级"
      this.addScoreFormVisible = true
      this.ScoreForm = {}
    },
    //清空校验规则
    clearInfo() {
      this.$refs.courseInfo.clearValidate()
    },
  },
}
</script>

<style scoped>
#search {
  width: 100%;
  height: 50px;
  border-bottom: 1px solid #eee;
}

#search, #header-bth, #nav-page {
  margin: 15px;
}

#role-table {
  width: 100%;
}
</style>
